﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Collections;
using System.Text.RegularExpressions;

namespace COM.PreAlarm.BLL
{
    public partial class MdBadWords
    {
        partial void OnBLLCreated() { }

        public static Dictionary<string, object> hash = new Dictionary<string, object>();
        public static BitArray firstCharCheck = new BitArray(char.MaxValue);
        public static BitArray allCharCheck = new BitArray(char.MaxValue);
        public static BitArray firstNegativeCharCheck = new BitArray(char.MaxValue);
        public static BitArray allNegativeCharCheck = new BitArray(char.MaxValue);
        public static int maxLength = 0;


        public static void InitFilter()
        {
            BaseDal b = new BaseDal();
            DataTable dtBadWords = b.GetDataTable("SELECT BadWords FROM dbo.mdBadWords WHERE  IsValid = 1"); ;
            if (dtBadWords == null || dtBadWords.Rows.Count == 0)
            {
                return;
            }

            foreach (DataRow dr in dtBadWords.Rows)
            {
                string word = StringHelper.FormatObjToString(dr["BadWords"]);
                if (!hash.ContainsKey(word))
                {
                    hash.Add(word, null);
                    maxLength = Math.Max(maxLength, word.Length);
                    firstCharCheck[word[0]] = true;

                    foreach (char c in word)
                    {
                        allCharCheck[c] = true;
                    }
                }
            }
        }

     

        public static string GetBadWords(string text)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            if (objCache["Cache_BadWords"] == null)
            {
                InitFilter();
            }

            //剔除非汉字，字母符号
            text = TrimCharacter(text);

            int index = 0;
            while (index < text.Length)
            {
                if (!firstCharCheck[text[index]])
                {
                    while (index < text.Length - 1 && !firstCharCheck[text[++index]]) ;
                }

                for (int j = 1; j <= Math.Min(maxLength, text.Length - index); j++)
                {
                    if (!allCharCheck[text[index + j - 1]])
                    {
                        break;
                    }

                    string sub = text.Substring(index, j);

                    if (hash.ContainsKey(sub))
                    {
                        return sub;
                    }
                }

                index++;
            }

            return string.Empty;

        }


       

        //剔除非汉字，非数字,非标点符号,非字母 [,-/.;:"'!]
        public static string TrimCharacterNew(string asString)
        {
            string sResult = "";
            string sCharacterRegx = "[^a-zA-Z0-9-,.;:'!?/，。；：‘！？\u4E00-\u9FFF]";
            Regex rgVal = new Regex(sCharacterRegx);
            sResult = rgVal.Replace(asString, "");
            return sResult;
        }


        //剔除非汉字，字母符号
        public static string TrimCharacter(string asString)
        {
            string sResult = "";
            string sCharacterRegx = "[^a-zA-Z0-9\u4E00-\u9FFF]";
            Regex rgVal = new Regex(sCharacterRegx);
            sResult = rgVal.Replace(asString, "");
            return sResult;
        }




    }





}